﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ashlen.Portal.Core;
using Ashlen.Portal.Core.DataInterfaces;

namespace Ashlen.Portal.Data.SqlServer
{
    public class FinanceDao : BaseDao, IFinanceDao
    {
        public FinanceDao() : base()
        {}

        public FinanceDao(string connectionString)
            : base(connectionString)
        {}


        #region IFinanceDao Members

        public object GetAccountsJSON() {
            RenewContext();
            var query = from a in Context.FinanceAccount
                        orderby a.AccountName
                        select new { a.ID, a.AccountName, a.Notes };
            var ret = query.ToList();
            return ret;
        }

        public object GetTransactionsJSON(string accountId) {
            RenewContext();
            Guid financeId = new Guid(accountId);
            var query = from t in Context.FinanceTransaction.Include("FinanceTransaction").Include("FinanceCategory")
                        where (t.FinanceAccount.ID == financeId)
                        orderby t.DateCreated descending
                        select new { t.ID, t.Date, t.DateCreated, t.DateUpdated, t.Amount, t.Description, t.FinanceCategory.Name,
                        t.Notes, t.Reconciled, t.Suspect, t.UserCreated, t.UserUpdated};
            var ret = query.ToList();
            return ret;
        }

        public object GetTransactionsByDateJSON(string accountId, DateTime startDate, DateTime endDate) {
            RenewContext();
            var query = from t in Context.FinanceTransaction
                        where t.FinanceAccount.ID == new Guid(accountId)
                        && t.DateCreated == startDate && t.DateCreated == endDate
                        orderby t.DateCreated descending
                        select new {
                            t.ID,
                            t.Date,
                            t.DateCreated,
                            t.DateUpdated,
                            t.Amount,
                            t.Description,
                            t.FinanceCategory.Name,
                            t.Notes,
                            t.Reconciled,
                            t.Suspect,
                            t.UserCreated,
                            t.UserUpdated
                        };
            var ret = query.ToList();
            return ret;
        }

        #endregion
    }
}
